﻿using System;
using System.IO;
using System.Xml.Serialization;
using Ant.ICommon.Utilities;
using Ant.Interface.GlobalInfo;

namespace Ant.Interface.Mef.Model.Catalogs.CatalogType
{
    /// <summary>
    /// 文件夹目录配置。
    /// </summary>
    public sealed class DirectoryCatalogConfig : CatalogConfigBase
    {
        #region "Constructor"

        public DirectoryCatalogConfig()
        {
            SearchOption = SearchOption.AllDirectories;
        }

        #endregion "Constructor"

        #region "Field"

        private string _directoryPath;

        #endregion "Field"

        #region "Property"

        /// <summary>
        /// 路径。
        /// </summary>
        public string DirectoryPath
        {
            get { return _directoryPath; }
            set
            {
                _directoryPath = string.IsNullOrWhiteSpace(value) ? null : value.Replace("{ApplicationPhysicalPath}", EnvironmentInfo.ApplicationPhysicalPath).Replace("{ApplicationPhysicalConfig}", EnvironmentInfo.ConfigPhysicalPath);
            }
        }

        /// <summary>
        /// 搜索表达式。
        /// </summary>
        public string SearchPattern { get; set; }

        /// <summary>
        /// 搜索选项。
        /// </summary>
        public SearchOption SearchOption { get; set; }

        #endregion "Property"

        #region "CatalogConfigBase Member"

        /// <summary>
        /// 目录类型。
        /// </summary>
        public override Mef.Model.Catalogs.CatalogType.CatalogType CatalogType
        {
            get { return Mef.Model.Catalogs.CatalogType.CatalogType.DirectoryCatalog; }
        }

        /// <summary>
        /// 组合部件。
        /// </summary>
        [XmlIgnore]
        public override object ComposablePart
        {
            get
            {
                var directory = new DirectoryInfo(DirectoryPath);

                //目录存在。
                if (!directory.Exists)
                    return null;

                //分隔搜索表达式。
                var searchPatterns = SearchPattern.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                if (Is.Empty(searchPatterns))
                    return null;

                var assemblyCatalogConfig = new AssemblyCatalogConfig();

                foreach (var searchPattern in searchPatterns)
                {
                    var files = directory.GetFiles(searchPattern, SearchOption);
                    foreach (var fileInfo in files)
                    {
                        assemblyCatalogConfig.GetAssemblyInfos.Add(new AssemblyCatalogConfig.GetAssemblyInfo
                                                                       {
                                                                           GetAssemblyMode = AssemblyCatalogConfig.GetAssemblyMode.AssemblyFile,
                                                                           Parameter = fileInfo.FullName
                                                                       });
                    }
                }
                return assemblyCatalogConfig.ComposablePart;
            }
        }

        #endregion "CatalogConfigBase Member"
    }
}